/*
 * 创建时间：17-9-23
 * 作者：sce
 * 功能描述：用户的model表
 * 修改时间：
 */

import Sequelize from 'sequelize'
import sequelize from './../../database'

const User = sequelize.define('sys_user', {
    id:{
        type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true
    },
    user_name:{
        type: Sequelize.STRING
    },
    user_no:{
        type:Sequelize.STRING,
        unique: true,
        allowNull: false
    },
    user_password:{
        type:Sequelize.STRING,
        allowNull: false
    },
    college:{
        type:Sequelize.STRING
    },
    class:{
        type:Sequelize.STRING
    },
    tel:{
        type:Sequelize.STRING
    },
    qq:{
        type: Sequelize.STRING
    },
    user_purview:{
        type: Sequelize.INTEGER,
        defaultValue:1,
        allowNull: false
    },
    lastlogin:{
        type:Sequelize.DATE
    }
},{
    freezeTableName:false,
    tableName: 'sys_user',
    updatedAt:'updated_at',
    createdAt:'created_at'
});
class UserModel{
    findByUserNo(user_no){
        return User.findOne({
            'where': {
                'user_no': user_no,
            },
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        });
    }
    regist(record){
        return User.create(record);
    }
    login(record){
        return User.findOne({
            'where': {
                'user_no': record.user_no,
                'user_password': record.user_password
            },
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        });
    }
    findUserById(id){
        return User.findById(id,{
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        });
    }
    updateUserInfo(record){
        return User.update(record,{
            where:{
              id: record.id
            },
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        });
    }
    updatePassword(record){
        return User.update(record,{
            where:{
                id:record.id
            },
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        });
    }
    findAllWithAdminByPage(record){
        return User.findAndCountAll({
            limit:record.page_size,
            offset: record.page_size * (record.page_no -1),
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        })
    }
    delUser(id){
       return User.destroy({
            where: {
                id: id
            },
            fields:['id','user_name','user_no','college','class','tel','qq','user_purview','lastlogin']
        });
    }
}
module.exports = new UserModel();